
package cn.edu.tsinghua.hpc.gsmsensor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Color;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.gsm.GsmCellLocation;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cn.edu.tsinghua.hpc.gsmtest.utils.LocationUtils;

public class PhoneState extends Activity {

    // private List<Map<String, Object>> mData;
    private ListView mListView = null;

    private ResourceCursorAdapter adapter;

    private MatrixCursor mCursor;

    private static final String ID = "_id";

    private static final String LAC = "LAC";

    private static final String CID = "CID";

    private static final String RX = "RX";

    private static final String TA = "TA";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mListView = new ListView(this);
        // mData = new ArrayList<Map<String, Object>>();

        mCursor = new MatrixCursor(new String[] {
                ID, LAC, CID, RX, TA
        });
        mCursor.addRow(new String[] {
                "0", LAC, CID, RX, TA
        });

        init();
        update();
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mCursor = new MatrixCursor(new String[] {
                        ID, LAC, CID, RX, TA
                });
                setTestData();

                mListView.postInvalidate();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setICC();
                        adapter.changeCursor(mCursor);
                    }
                });
            }
        }, 0, 1 * 1000);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void update() {
        adapter = new ResourceCursorAdapter(this, R.layout.phone_state_item, mCursor, true) {

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                if (cursor == null || view == null)
                    return;
                ((TextView) view.findViewById(R.id.col1)).setText(cursor.getString(cursor
                        .getColumnIndex(LAC)));
                ((TextView) view.findViewById(R.id.col2)).setText(cursor.getString(cursor
                        .getColumnIndex(CID)));
                ((TextView) view.findViewById(R.id.col3)).setText(cursor.getString(cursor
                        .getColumnIndex(RX)));
                ((TextView) view.findViewById(R.id.col4)).setText(cursor.getString(cursor
                        .getColumnIndex(TA)));

                String lac = ((TextView) mListView.findViewById(R.id.editLAC)).getText().toString();
                String cid = ((TextView) mListView.findViewById(R.id.editCID)).getText().toString();
                if (lac.equals(cursor.getString(cursor.getColumnIndex(LAC)))
                        && cid.equals(cursor.getString(cursor.getColumnIndex(CID)))) {
                    view.setBackgroundColor(Color.BLUE);
                } else {
                    view.setBackgroundColor(Color.BLACK);
                }

            }
        };

        mListView.setAdapter(adapter);
        setContentView(mListView);
    }

    private void init() {

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.phone_position_item, null);
        mListView.addFooterView(layout);
        View layout1 = inflater.inflate(R.layout.phone_icc_item, null);
        mListView.addFooterView(layout1);

    }

    private void setICC() {

        TextView imei = (TextView) mListView.findViewById(R.id.contentIMEI);
        imei.setText(LocationUtils.getInstance(this).getIMEI());
        TextView imsi = (TextView) mListView.findViewById(R.id.contentIMSI);
        imsi.setText(LocationUtils.getInstance(this).getIMSI());
    }

    private void setTestData() {
        addStationInfo(LAC, CID, RX, TA);
        GsmCellLocation loc = LocationUtils.getInstance(this).getCellLocation();
        addStationInfo(loc.getLac(), loc.getCid(), 0, 0);
        for (NeighboringCellInfo info : LocationUtils.getInstance(this).getNeighboringCellInfo()) {
            addStationInfo(info.getLac(), info.getCid(), info.getRssi(), 1);
        }
    }

    private void addStationInfo(int lac, int cid, int rx, int ta) {
        mCursor.addRow(new String[] {
                String.valueOf(mCursor.getCount()), String.valueOf(lac), String.valueOf(cid),
                String.valueOf(rx), String.valueOf(ta)
        });
    }

    private void addStationInfo(String lac, String cid, String rx, String ta) {
        mCursor.addRow(new String[] {
                String.valueOf(mCursor.getCount()), lac, cid, rx, ta
        });
    }

}
